<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 粤宝云
 */
class YuebaoyunController extends PayController{
    protected $config;

    public function pay($array){
        $this->config = M("Channel")->where(array('id'=>$array['channel']))->find();
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'012']);
        }

        $product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'012']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['mch'] = $this->config['mch_id'];

        switch($this->config['paytype']){
            //支付宝H5
            case 4:
                $data['pay_type'] = 'aliwap';
                break;
            //微信
            case 1:
                $data['pay_type'] = 'wxqrcode';
                $data['create_ip'] = $_SERVER['REMOTE_ADDR'];
                break;
            //支付宝
            case 3:
                $data['pay_type'] = 'aliqrcode';
                break;
                //微信H5
            case 2:
                $data['pay_type'] = 'wxhtml';
                $data['create_ip'] = $_SERVER['REMOTE_ADDR'];
                break;
            //qq
            case 8:
                $data['pay_type'] = 'qqqrcode';
                break;
            //qqwap
            case 10:
                $data['pay_type'] = 'qqweb';
                $data['create_ip'] = $_SERVER['REMOTE_ADDR'];
                break;
            //银联
            case 11:
                $data['pay_type'] = 'ylggp';
                break;
            //银联快捷
            case 13:
                $data['pay_type'] = 'ylpay';

                $data['card_type'] = 1;
                $data['yl_pay_type'] = 'B2C';
                $data['bank_name'] = '工商银行';
                $data['create_ip'] = $_SERVER['REMOTE_ADDR'];
                break;
            //京东
            case 9:
                $data['pay_type'] = 'jdqrcode';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>'012']);
        }
        $data['money'] = intval($return['amount']*100);
        $data['time'] = time();
        $data['order_id'] = $return['orderid'];
        $data['return_url'] = $return['callbackurl'];
        $data['notify_url'] = $return['notifyurl'];
        $data['extra'] = 'zf';

        $str = "{$data['order_id']}{$data['money']}{$data['pay_type']}{$data['time']}{$data['mch']}".md5 ($this->config['signkey']);
        $data['sign'] = md5($str);

        $apiurl = $this->config['gateway'] . "?";
        foreach($data as $k=>$v){
            $apiurl .= "{$k}={$v}&";
        }
        $apiurl = substr($apiurl,0,-1);

        if(in_array($this->config['paytype']  , [2,4,10])){
            header("location: {$apiurl}"); exit;
        }

        $res = curl_get($apiurl);
        if(!$res){
            $this->showmessage('获取数据失败!',['code'=>'012']);
        }
        $res = json_decode($res, true);
        if(!$res || empty($res)){
            $this->showmessage('获取的数据格式错误!',['code'=>'012']);
        }

        $qrcode = $res['data']['img'];
        if(!$qrcode){
            echo '<pre/>'; print_r($res);exit;

            $this->showmessage('获取二维码失败!',['code'=>'012']);
        }

        $paytype = transArr( C("PAYTYPES"), "id");

        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", $qrcode);
        $this->display("WeiXin/Pay");

    }

    //通知
    public function notifyurl(){
        $p = $_POST;

        //获取订单信息
        $orderid = $p['order_id'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> json_encode($p),
            "pay_orderid_rd"=>$p['orderNo']
        ));
        Order::getInstance()->edit($find['id'],array("server_return"=> json_encode($p), "pay_orderid_rd"=>$p['orderNo']));
        Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array("server_return"=> json_encode($p), "pay_orderid_rd"=>$p['orderNo']));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Yuebaoyun'))->find();

        if(!$this->config){
            exit("get_tongdao_fail");
        }

        if(!$p || empty($p)){
            exit("wrong");
        }

        if($p['status'] != 1){
            exit("pay_fail");
        }

        $sign = $p['sign'];

        $str = "{$p['order_id']}{$p['orderNo']}{$p['money']}{$p['mch']}{$p['pay_type']}{$p['time']}".md5 ($this->config['signkey']);
        $sign2 = md5($str);

        if($sign != $sign2){
            exit("sign_wrong");
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Yuebaoyun",0);

        //回调成功
        echo 'success'; exit;
    }

    public function callBackUrl(){
        echo '处理成功'; exit;
    }
}